package com.atguigu.gmall.payment;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

/**
 * @author 李旭
 * @date 2021/10/27 10:32
 * @Description: 使用SDK远程调用 支付宝公司对外暴露的网关 接口
 */
public class AlipayDemo {

    //连接支付宝公司网关 接口
    public static void main(String[] args) {
        //1、实例化客户端
        //公共参数
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",//1:网关地址
                "2016102100732915",  //2:APPID
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCARXC1B4ivrnYasExbFXiGF0yolA/WZd4Q74WyH8thmLiKkq436jQS39tKYrjiKRuZe5rRLh7TFZF04j1aFqNys/v5KkqffE3mPUYolLTYXe/9c6oood/ZaQE1okMCHQ8+sPSES1/Yh54X4NO3wBJPKmBL6rnmllho7zxxEMlkx1/UXIS0g7+AUr1Suh0APZ6MsG56Q/EUVH+FH0XMT8yLRn7DPm6ivIScZ0yDG0lP/8s+lDs/cU5WZzdmcn/9v9Yx+xN+OR52D8b9AxeUB1Bd7onV3WE9WWRJwQxfW78KBxWTAOsy3M5E2aaOC0MhYRmKMhHhx3IU/eF0sTvT67M/AgMBAAECggEAHkfGPoYDx21I5i3+KHZeGFGBwD4LqxIrJRZWGmGVEnoJ/nR3+i0ok9gSnXHGsWUA5e+e8YiQsEXkK5KI5y528Un5zq0kJpgij5qhaE0KkBVE5TEb1zG3dUxrg6zFYsdXZPZDEsP0YVpKgNqLQFJwQA4B44oKzptpTYREYXCUCl0MQ6+xyjVgHKP+2MkIlsuEfEXzsTtpeN6++aTa5fMI71v9hUdhwgz6zbwWaKsjISV7WewsQvKcAIy7uYLw7nCrsgR64TDo3vJ8FKwxI14Nchia9/RFUaotL8xEUF4WGI9ftIHRsQcV1QNV/fXT09YUbyPZaI9BdxCAZcBdHbZQiQKBgQDOWkdzl/09N5WIK+S+GF3yNV+vQyr4bBry+/f+TUgZ1iOrQiPqRQK9rghxZMjrdC52zE+lSWkauzQTJ4l6d2ZByTBfKMxB0BPf0AkZMxtndLM+Flxg9muFx6NN0/3Gv43uuNgFUBOGPHkTIT1EvdqWJFR2g2pAPWmGo9cqmpPa2wKBgQCfIfQDsX8B/opIv0AtDV4fFQEGDN4eQ5Tw66T+ve05gpo7Yen3oD/T03d9/tcT3zdeFiy4ymqCOAVZ66Jf7ol2+ivv029PUnLLG/Yspsv14asz6TcUqgWAOS+H1EiRG+9gVreyMgA4U/Hs6CISrlpYORFoIc6oXRT78nq94APMbQKBgB/eD1TJryEqGVyrMqssxi1mkr1VSSiOQWcvBHBwbOcYi++hyjwepjq2yrUAVIsQKTDNgqtK+AFZ0cP3giK4l8EnGeJIQasUQE17KjyLCPtkKV44Q9HxnE9PsmeNio6L/JZWU34WjL1ZT3Vl17lq/HvdZnHa/6imE0rouIzqzFSrAoGAY3nOGCD0zEJaH2+upnscw0FWCnowv6pviMtFsRj5GmJSF5z1rR43yCsBH3oW6Q04h1BFVJ70RcLPVY6/SkjLFdz9akY55tQbfmxUT0VVardPVnwLOpeuKKGW3MoPMubLlOWgn3wyNwQCz52s7xCe2VeTUg0QKp9FZtoFcHyA42ECgYAzqfvkPOCXUnnzQpk+f+iKX4c/zR+mu3zgYTVOM2hdcPge2CG743mjknEO+7gNyLmniHE5PwDFqXz0KwwkqSQlCjqhuZZJZrr+W/gtbZdi021a8OmZmTPDOTtsEOyqSx/53OlhFjsN1kxW8JLayuPOfs5bB9PdOQX1D/e9mrehjg==", //3:商家的私钥  （签名）
                "json", //5:发送的数据的格式
                "UTF-8",//6:数据的编码
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv4FgjWsfvbXEkzs5T+z5r4X9m5Qh8OA0zlZA3T9yeCqlcRRqydMBfRsB4V9aHUH0KCV875mzF7Feh3VoDilcDpGtuKo5l7YRVnGSyQTq0pQjMFYi/wKGFGHAA37Wl2L/AMaGpT6CJncSqcAJssY61qmz/o9lw+hRq6KBfdktl4xm0d5f+uMgCLKqa6bYbFyG7vNZszGJ6yW2n6fFbZ0B4PgPXfrRFVG+OS204bi7sGDba6DuJPPuk8B/gkVoDkmKHNRnLXZi74jyZROlEXKr8JElZqpT6RODkKAmIRCGlQrAadUntgmymguRlN2K+TfSNIigY1FRekb/BudYhlbA5QIDAQAB",//4:支付宝公司的公钥
                "RSA2" //7:加密方式
        );
       //2: 接口名称  统一收单下单并支持接口 alipay.trade.page.pay
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //TODO 异步回调
        //request.setNotifyUrl("");
        //TODO 同步回调
        //request.setReturnUrl("");

        //请求参数
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "20210817010101004123");//支付宝 交易编号 唯一 必选
        bizContent.put("total_amount", 100000000);//总金额  一个亿必选
        bizContent.put("subject", "测试商品手机");//必选
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");//注：目前仅支持FAST_INSTANT_TRADE_PAY必选
        //Calendar calendar  = Calendar.getInstance();
        //calendar.add(Calendar.HOUR,1);
        //bizContent.put("time_expire", calendar.getTime());//二维码的  绝对时间  16:30  可选

        System.out.println("json:" + bizContent.toJSONString());
        System.out.println("String:" + bizContent.toString());

        request.setBizContent(bizContent.toString());
        //3:发出此请求
        AlipayTradePagePayResponse alipayTradePagePayResponse = null;
        try {
            alipayTradePagePayResponse = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        if(alipayTradePagePayResponse.isSuccess()){
            System.out.println("调用成功");
            System.out.println(alipayTradePagePayResponse.getBody());
        }else{
            System.out.println("调用失败");
        }

    }
}
